package com.tiance.demo.ui.status;

import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;

import java.util.regex.Pattern;

public class BottomBar extends HBox {

    private static int base = 0;

    public BottomBar() {
        this.setPrefHeight(30);
        this.setStyle("-fx-background-color: #FFE4C4");
        this.setPadding(new Insets(4, 10, 4, 10));

        Label label2 = new Label("二进制:");
        Label label10 = new Label("十进制:");
        Label label16 = new Label("十六进制:");

        TextField textField2 = new TextField("") {
            @Override
            public void replaceText(int start, int end, String text) {
                String oldOne = getText();
                if (Pattern.matches("[01]*", text)) {
                    super.replaceText(start, end, text);
                } else {
                    setText(oldOne);
                    this.end();
                }
            }
        };
        textField2.setOnKeyTyped((e) -> {
            if (base != 2) base = 2;
        });
        textField2.setPrefSize(200, 10);
        textField2.setFont(Font.font(10));

        TextField textField10 = new TextField("") {
            @Override
            public void replaceText(int start, int end, String text) {
                String oldOne = getText();
                if (Pattern.matches("[0-9]*", text)) {
                    super.replaceText(start, end, text);
                } else {
                    setText(oldOne);
                    this.end();
                }
            }
        };
        textField10.setPrefSize(200, 10);
        textField10.setFont(Font.font(10));
        textField10.setOnKeyTyped((e) -> {
            if (base != 10) base = 10;
        });

        TextField textField16 = new TextField("") {
            @Override
            public void replaceText(int start, int end, String text) {
                String oldOne = getText();
                if (Pattern.matches("[0-9a-fA-F]*", text)) {
                    super.replaceText(start, end, text.toUpperCase());
                } else {
                    setText(oldOne);
                    this.end();
                }
            }
        };
        textField16.setPrefSize(200, 10);
        textField16.setFont(Font.font(10));
        textField16.setOnKeyTyped((e) -> {
            if (base != 16) base = 16;
        });

        Button button = new Button("计算");
        button.setFont(Font.font(12));
        button.setOnMouseClicked((e) -> {
            if (base == 2) {
                String text2 = textField2.getText();
                if (text2 == null || text2.length() == 0) {
                    return;
                }
                text2 = text2.replaceAll("\\s*", "");
                int int10 = Integer.parseInt(text2, 2);
                textField10.setText(String.valueOf(int10));
                textField16.setText(Integer.toHexString(int10).toUpperCase());
            } else if (base == 10) {
                String text10 = textField10.getText();
                if (text10 == null || text10.length() == 0) {
                    return;
                }
                text10 = text10.replaceAll("\\s*", "");
                textField2.setText(Integer.toBinaryString(Integer.parseInt(text10)));
                textField16.setText(Integer.toHexString(Integer.parseInt(text10)).toUpperCase());
            } else if (base == 16) {
                String text16 = textField16.getText();
                if (text16 == null || text16.length() == 0) {
                    return;
                }
                text16 = text16.replaceAll("\\s*", "");
                int int10 = Integer.parseInt(text16, 16);
                textField10.setText(String.valueOf(int10));
                textField2.setText(Integer.toBinaryString(int10));
            }
        });

        getChildren().addAll(label2, textField2, label10, textField10, label16, textField16, button);
    }
}
